// const https = require('https')
const sd = require("silly-datetime");
const request = require('superagent');

module.exports = async (ctx) => {
  // let url = 'https://api.douban.com/v2/book/'+'1220561'
  let url = 'https://route.showapi.com/341-3?maxResult=20&page=3&showapi_appid=77211&showapi_sign=c9eb0bdd51264a77b6638f8cb15769cf'
  const bookinfo = await getJoke(url)
  // console.log('bookinfo', bookinfo);
  console.log('data', sd.format(new Date(),'YYYYMMDDHHmmss'))
  ctx.state.data = {
    'msg': JSON.parse(bookinfo.text)
  }
}

function getJoke(url) {
  return new Promise((reslove, reject) => {
    request('GET', url).then(res => {
        // const bookinfo = JSON.parse(res.data)
        const bookinfo = res
        if(bookinfo){
          reslove(bookinfo)
        }
        reject(bookinfo)
      }
    )
  })
}